<template>
  <div class="attr-info">
    <el-dialog
      :title="info.title + '规格'"
      :visible.sync="info.isShow"
      width="50%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="属性名" prop="specsname">
          <el-input v-model="ruleForm.specsname"></el-input>
        </el-form-item>
        <el-form-item label="属性值">
          <div class="attr-div" v-for="(item, index) in attrList" :key="index">
            <el-input class="attr-input" v-model="attrList[index]"></el-input>
            <el-button v-if="index" type="danger" @click="delAttr(index)">删除属性</el-button>
            <el-button v-else type="primary" @click="addAttr">添加属性</el-button>
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            class="btn"
            v-if="info.title === '添加'"
            type="primary"
            @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button
            class="btn"
            v-if="info.title === '修改'"
            type="primary"
            @click="submitForm('ruleForm')"
            >立即修改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addSpecsFn, getMenuTreeListFn, editRoleFn } from "../../apis";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        specsname: "",
        attrs: '',
        status: 1,
      },
      attrList: [''],
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    // 发请求，获取菜单
    this.getMenuList();
  },
  methods: {
    setValue(data) {
      // 设置弹窗中表单的值
      // 深拷贝，避免数据影响
      this.ruleForm = { ...data };
      this.attrList = [...this.ruleForm.attrs];
    },
    async getMenuList() {
      let res = await getMenuTreeListFn();
      // 给下拉框的数组赋值
      this.menuList = res.list; 
    },
      // this.$refs[formName]获取表单的dom节点
    submitForm(formName) {
      // validate 校验函数，element 自带的函数
      this.$refs[formName].validate((valid) => {
        // valid 校验的结果
        if (valid) {
          // 整理数据，将attrList数组中的数据赋值给ruleForm
          this.ruleForm.attrs = this.attrList.join(',')
          this.add();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 真的添加了
    async add() {
      let res;
      if (this.info.title === "添加") {
        res = await addSpecsFn(this.ruleForm);
      } else {
        res = await editRoleFn(this.ruleForm);
      }
      console.log(res);
      if (res.code === 200) {
        this.closed();
        // 通知list组件更新数据
        this.$emit("update");
        // 提示成功
        this.$message({
          type: "success",
          message: res.msg,
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹窗
    closed() {
      this.info.isShow = false;
    },
    // 添加属性
    addAttr() {
      if(this.attrList.length <= 4) {
        this.attrList.push('')
      }else {
        this.$message({
          message: '属性值最多5个',
          type: 'warning'
        })
      }
    },
    // 删除属性
    delAttr(index) {
      this.attrList.splice(index, 1);
    }
  },
};
</script>

<style>
  .attr-info .attr-input {
    width: 200px;
    margin-right: 20px;
  }
  .attr-info .attr-div {
    margin-top: 10px;
  }
</style>